package org.yccloud.yc.demo.kt.controller

import org.springframework.web.bind.annotation.*
import org.yccloud.yc.demo.kt.model.Message
import org.yccloud.yc.demo.kt.model.MessagePO
import org.yccloud.yc.demo.kt.service.MessageDBService
import org.yccloud.yc.demo.kt.service.MessageService

@RestController
class MessageController(
    val service: MessageService,
    val dbService: MessageDBService
) {

    @GetMapping("/")
    fun indx(@RequestParam("name") name: String) = "Hello, $name"

    @GetMapping("/list")
    fun list() = listOf(
        Message("1", "Hello"),
        Message("2", "Bonjour!"),
    )

    @GetMapping("/msg")
    fun getMsg(): List<Message> = service.findMessage()

    @PostMapping("/save_msg")
    fun saveMsg(@RequestBody message: Message) {
        service.save(message)
    }

    @GetMapping("/db/msg")
    fun getDbMsg(): List<MessagePO> = dbService.findMessages()

    @GetMapping("/db/{id}")
    fun getMsgById(@PathVariable id: String): List<MessagePO> = dbService.findMessageById(id)
}